#pragma once

#include <iostream>

#include <cstdlib>
#include <ctime>
#include <random>

#include <gdstk/gdstk.hpp>
#include "qlib/all.h"

using namespace qlib;

// 生成随机数
inline double rand_double(int range = 1000) {
  std::random_device rd;
  std::mt19937 gen(rd());
  // 产生随机数: [-range*1000, range*1000]-int
  std::uniform_int_distribution<> dis(-range * 1000, range * 1000);

  int random_number = dis(gen);
  /// 返回随机数：[-range, range]-double
  return random_number / 1000.0;
}

// 生成随机浮点数组
inline Array<double> rand_double_array(int size = 100, int range = 1000) {
  Array<double> res;
  res.resize(size);

  for (size_t i = 0; i < size; i++) {
    res[i] = rand_double(range);
  }

  return res;
}

// 生成随机二维点数组
inline Array<Vec2d> rand_vec2_array(int size = 100, int range = 1000) {
  Vec2dArray res;
  res.resize(size);

  for (size_t i = 0; i < size; i++) {
    res[i].x = rand_double(range);
    res[i].y = rand_double(range);
  }

  return res;
}

// 将qlib浮点数组转换为gdstk数组
inline gdstk::Array<double> to_gds(const std::vector<double>& vector) {
  gdstk::Array<double> res = {0, 0, NULL};
  res.ensure_slots(vector.size());

  for (size_t i = 0; i < vector.size(); i++) {
    res[i] = vector[i];
  }

  res.count = vector.size();
  return res;
}

// 将qlib二维点数组转换为gdstk数组
inline gdstk::Array<gdstk::Vec2> to_gds(const std::vector<Vec2d>& vector) {
  gdstk::Array<gdstk::Vec2> res = {0, 0, NULL};
  res.ensure_slots(vector.size());

  for (size_t i = 0; i < vector.size(); i++) {
    res[i].x = vector[i].x;
    res[i].y = vector[i].y;
  }

  res.count = vector.size();
  return res;
}